package rickyGame.game.action.ai
{
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	import ricky.quadtree.Leaf;
	import ricky.quadtree.Morton;
	import ricky.quadtree.Quadtree;
	import ricky.utils.FrameManager;
	import ricky.utils.Utils;
	
	import rickyGame.model.config.*;
	import ricky.game.Actor2D;
	import rickyGame.game.GameState;
	import rickyGame.game.Player;
	import rickyGame.game.action.MoveLeafAction;
	import rickyGame.game.scene.Pathfinder;
	
	/**
	 * 游戏ai之四处游逛
	 * 如果怪物没事可做就四处游荡
	 */
	public class WanderAi extends MoveLeafAction
	{
		public function WanderAi(actor:Actor2D, par:Object=null)
		{
			super(actor);
			_range=(actor.data["maxrangeA"] as Number);
			this._state=AiType.WanderAi;
			this._animProperty="run";
			_calcPoint=new Vector3D();
			
			_localX=Player(actor).ai.localX;
			_localY=Player(actor).ai.localY;
			
			this._type="WanderAi";
				
		}
		
		override public function begin():void
		{
			_lastTime=new Date().getTime();
			if(this.findTarget())
			{
				moveStep();
				calculateDirection();
			}
			_last=_lastTime;
			FrameManager.inst.addLogic(getActor());
		}
		
		override public function action(timeline:Number):void
		{
			var dt:Number=timeline-_last;
			if(this._isfindTarget==false && dt>= 1200)
			{
				_last=timeline;
				findTarget();
				moveStep();
				calculateDirection();
			}
			super.action(timeline);
		}
		
		override protected function  onGridChange():void
		{
			super.onGridChange();
			
//			checkTransition();
		}
		
		/**
		 * 行动完了
		 */
		override public function end():void
		{
			FrameManager.inst.removeLogic(getActor());
//			super.end();
			var m:int=Math.random()*100;
			if(m<60) //50几率 空闲
			{
				var idle:IdleAi=new IdleAi(Actor2D(actor));
				Actor2D(actor).addAct(idle);
			}else
			{
				this.begin();//重新开始
			}

		}
		
//		override public function checkTransition():void
//		{
//			var rang:int=actor.data.maxrangeA;
//			var me:Player=GameState.inst.me;
//			var dis:int=Utils.gridDistance1(me.gridX,me.gridY,this.actor.gridX,this.actor.gridY);
//			
//			if(dis<rang)
//			{
//				
//			}
//		}
		
		/**
		 * 寻找范围内可以到达的路径
		 */
		protected function findTarget():Boolean
		{
			var l:Leaf;
			
			var gridx:int=_localX/40;
			var gridy:int=_localY/30;
			var tx:int=gridx-_range+(Math.random()*_range*2);
			var ty:int=gridy-_range+(Math.random()*_range*2);
			
			if(tx<=0)
				tx=0;
			if(ty<=0)
				ty=0;

			var m:int=Morton.morton(ty,tx);
			
			l=GameState.inst.scene.tree.getLeaf(m);
			
			if(l)
			{
			 	var tree:Quadtree=GameState.inst.scene.tree;
				var me:Leaf=tree.getLeaf(Actor2D(_actor).morton);
				var arr:Array=Pathfinder.findPath1(me,l,tree,Pathfinder.traversableAroundFunction,_actor.asType);
				_path=arr;
				_isfindTarget= true;
			}
			else
			{
				_isfindTarget= false;
				_animProperty="stand";
			}
			
			return _isfindTarget;
		}
		
		//
		protected var _range:Number;
		protected var _isfindTarget:Boolean=false;

		protected var _localX:Number;
		protected var _localY:Number;
		
		protected var _last:Number;
	}
}